/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.core.model.attribute;

import java.lang.reflect.Constructor;

import org.apache.log4j.Logger;

import org.rails.core.GLOBAL;
import org.rails.core.model.ActiveRecord;
import org.rails.core.resource.ConfigBuilder;
import org.rails.core.resource.ResourceFactory;
import org.rails.core.util.Utils;

public final class Attribute {

	private final Logger logger = Logger.getLogger(this.getClass());
	
	private final String ATTRIBUTE_CLASS = "org.rails.core.model.attribute.{0}Attribute";

	private final static ConfigBuilder config = ResourceFactory.getConfigBuilder();
	private ActiveRecord record;
	private String typeKey;	
	private String name;
	private String type;

	private Object value;

	private AttributeBase base;

	public Attribute(ActiveRecord record, String name) {
		this.record = record;
		this.name = name;
		init();
	}

	@SuppressWarnings("unchecked")
	private void init() {
		typeKey = Utils.format("{0}.{1}.type",record.getClass().getSimpleName(), name);
		if (!isConfig()) {
			typeKey = Utils.format("{0}.{1}.type",GLOBAL.SYSTEM_DEFAULT_MODEL, name);
		}
		if (isConfig()) {
			type = config.get(typeKey);
			try {
				final Class<AttributeBase> c = (Class<AttributeBase>) Class.forName(Utils.format(
						ATTRIBUTE_CLASS, type));
				@SuppressWarnings("rawtypes")
				final Constructor con = c.getConstructor(ActiveRecord.class, String.class);
				con.setAccessible(true);
				base = (AttributeBase) con.newInstance(record, name);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}
	
	public boolean isConfig(){
		return config.containsKey(typeKey);
	}

	public Object getDefaultValue() throws AttributeException {
		if(base == null)
			return null;
		else
			return base.getDefaultValue();
	}

	public String getType() {
		return type;
	}

	public String getName(){
		return name;
	}
	
	public String getModel(){
		return record.getClass().getSimpleName();
	}
	
	public Object getValue() {
		if (base == null) 
			return value;
		else
			return base.getValue();
	}

	public boolean isUnique() {
		if(base == null)
			return false;
		else
			return base.isUnique();
	}

	public String getMessage() {
		if(base == null)
			return null;
		else
			return base.getMessage();
	}

	public String getFormat() {
		if(base == null)
			return null;
		else
			return base.getFormat();
	}
	
	public String getCondition() {
		if(base == null)
			return null;
		else
			return base.getCondition();
	}

	public boolean isTrim() {
		if(base == null)
			return false;
		else
			return base.isTrim();
	}

	public boolean isRequire() {
		if(base == null)
			return false;
		else
			return base.isRequire();
	}

	public Object getMin() throws AttributeException {
		if(base == null)
			return null;
		else
			return base.getMin();
	}

	public Object parse(Object arg) throws AttributeException {
		if (base == null) {
			return arg;
		} else {
			return base.parse(arg);
		}
	}

	public String format(Object arg) {
		if (base == null) {
			return arg == null ? null : arg.toString();
		} else {
			return base.format(arg);
		}
	}

	public void error() throws AttributeException {
		if (base == null) 
			this.value = record.get(name);			
	    else 
			base.validate();		
	}

	public Object getMax() throws AttributeException {
		if(base == null)
			return null;
		else
			return base.getMax();
	}
	
	public ActiveRecord getRecord(){
		return record;
	}
	
	public Object getInputValue(){
		if (base == null) 
			return null;
		else
			return base.getInputValue();
	}
	
	public void setExceMsg(String exceMsg){
		if(base != null)
			base.setExceMsg(exceMsg);
	}
	
	public String getExceMsg(){
		if(base == null)
			return null;
		else
			return base.getExceMsg();
	}

}
